package com.liqing.movecomment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liqing.movecomment.dao.MycollectionDao;
import com.liqing.movecomment.entity.Mycollection;
import com.liqing.movecomment.entity.User;
import com.liqing.movecomment.exception.LdException;
import com.liqing.movecomment.service.MoveService;
import com.liqing.movecomment.service.MycollectionService;
import com.liqing.movecomment.util.SwapePageUtil;
import com.liqing.movecomment.util.ThreadLocalUtils;
import com.liqing.movecomment.vo.CollectionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Transactional
@Service("mycollectionService")
public class MycollectionServiceImpl implements MycollectionService {
    @Resource
    private MycollectionDao mycollectionDao;


    @Resource
    private MoveService moveService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CollectionVo queryById(Integer id) {
        return copyEToVo(this.mycollectionDao.selectById(id));

    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 页大小
     * @return 查询结果
     */
    @Override
    public PageInfo<CollectionVo> queryAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        User user = (User) ThreadLocalUtils.get();

        LambdaQueryWrapper<Mycollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Mycollection::getUserId, user.getId());
        List<Mycollection> list = this.mycollectionDao.selectList(wrapper);


        List<CollectionVo> collectionVos = copyList(list);


        return SwapePageUtil.swapePage(list, collectionVos);
    }

    private List<CollectionVo> copyList(List<Mycollection> mycollectionList) {
        List<CollectionVo> list = new ArrayList<>();
        mycollectionList.stream().parallel().forEach(mycollection -> {
            list.add(copyEToVo(mycollection));
        });


        return list;
    }

    private CollectionVo copyEToVo(Mycollection mycollection) {
        CollectionVo collectionVo = new CollectionVo();
        BeanUtils.copyProperties(mycollection, collectionVo);
        collectionVo.setMoveVo(moveService.queryById(mycollection.getMoveId()));
        return collectionVo;
    }

    /**
     * 新增数据
     *
     * @param mycollection 实例对象
     * @return 实例对象
     */
    @Override
    public Mycollection insert(Mycollection mycollection) {
        final LambdaQueryWrapper<Mycollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Mycollection::getMoveId,mycollection.getMoveId()).eq(Mycollection::getUserId,mycollection.getUserId());
        final Mycollection mycollection1 = mycollectionDao.selectOne(wrapper);

        if(ObjectUtils.isNotEmpty(mycollection1)){
            throw new LdException(1,"你已收藏过，无需再收藏");
        }

        this.mycollectionDao.insert(mycollection);
        return mycollection;
    }

    /**
     * 修改数据
     *
     * @param mycollection 实例对象
     * @return 实例对象
     */
    @Override
    public Mycollection update(Mycollection mycollection) {
        this.mycollectionDao.updateById(mycollection);
        return this.mycollectionDao.selectById(mycollection.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.mycollectionDao.deleteById(id) > 0;
    }
}
